package com.zimin.cloud.user.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zimin.cloud.mine.pojo.user.business.UserBO;
import com.zimin.cloud.mine.pojo.user.business.UserInfoBO;
import com.zimin.cloud.mine.pojo.user.view.UserInfoVO;
import com.zimin.cloud.user.biz.IUserBiz;
import com.zimin.cloud.user.biz.IUserFollowingBiz;
import com.zimin.cloud.user.common.util.ServletUtil;
import com.zimin.cloud.user.controller.api.IUserFollowingController;
import com.zimin.cloud.user.pojo.entity.UserInfo;
import com.zimin.common.utils.ObjectUtils;
import com.zimin.common.utils.RSAUtil;
import com.zimin.shared.pojo.PageVO;
import com.zimin.shared.pojo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import com.zimin.shared.pojo.BaseController;
import com.zimin.cloud.user.controller.api.IUserController;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author zimin
 * @since 2022-06-14 16:10
 */
@RestController
public class UserController extends BaseController implements IUserController {

    @Autowired
    private IUserBiz userBiz;
    @Autowired
    private IUserFollowingBiz userFollowingBiz;

    @Override
    public Result getUserInfo() {
        String userId = ServletUtil.getCurrentUserId();
        return Result.ok(userBiz.getUserInfo(userId));
    }

    @Override
    public Result getRsaPublicKey() {
        var publicKeyStr = RSAUtil.getPublicKeyStr();
        return Result.ok(publicKeyStr);
    }

    @Override
    public Result addUser(UserBO userBO) {
        return result(userBiz.addUser(userBO));
    }

    @Override
    public Result login(UserBO userBO) throws Exception {
        var token = userBiz.login(userBO);
        return Result.ok(token);
    }

    @Override
    public Result updateUserInfos(UserInfoBO userInfoBO) throws Exception {
        var userId = ServletUtil.getCurrentUserId();
        userInfoBO.setId(userId);
        return Result.ok(userBiz.updateUserInfos(userInfoBO));
    }


    @Override
    public Result getUserInfos(Page<UserInfo> page, UserInfoBO userInfoBO) {
        var userId = ServletUtil.getCurrentUserId();
        userInfoBO.setId(userId);
        PageVO<UserInfoVO> userInfoVOPageVO = userBiz.pageListUserInfos(page, userInfoBO);
        if(ObjectUtils.isNotEmpty(userInfoVOPageVO)){
            var checkedUserInfoList = userFollowingBiz.checkFollowingStatus(userInfoVOPageVO.getRecords(),userId);
            userInfoVOPageVO.setRecords(checkedUserInfoList);
        }
        return Result.ok(userInfoVOPageVO);
    }

    @Override
    public Result loginForDts(UserBO userBO) throws Exception {
        var map = userBiz.loginForDts(userBO);
        return result(map);
    }

    @Override
    public Result logout() throws Exception {
        String refreshToken = ServletUtil.getRequest().getHeader("refreshToken");
        var userId = ServletUtil.getCurrentUserId();
        userBiz.logout(refreshToken,userId);
        return Result.ok();
    }

    @Override
    public Result refreshAccessToken() throws Exception {
        var refreshToken = ServletUtil.getRequest().getHeader("refreshToken");
        var accessToken =  userBiz.refreshAccessToken(refreshToken);
        return Result.ok(accessToken);
    }


}
